package cc.rengu.igas.cops.core.service.trans.intelliroute;

import cc.rengu.igas.cops.common.constant.CopsProductVersion;
import cc.rengu.igas.cops.common.entity.intelli.request.IntelliRouterQueryRequest;
import cc.rengu.igas.cops.common.enums.CopsRespCodeEnum;
import cc.rengu.igas.cops.core.model.ProcAddition;
import cc.rengu.igas.route.facade.request.SmartRouteRequest;
import cc.rengu.igas.route.facade.response.SmartRouteResponse;
import cc.rengu.igas.route.facade.result.Result;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.*;

import java.util.Optional;

/**
 * 路由查询交易
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/05/02
 */
public class IntelligenceRouterQueryService extends BizBaseService {
    private static final String DUBBO_BEAN = "smartRouteService";
    private static final String DUBBO_METHOD = "smartRouteChoice";

    @Override
    protected Object beforeBizProcess() throws Exception {
        IntelliRouterQueryRequest request = new IntelliRouterQueryRequest();
        ConvertUtil.convertOutput(request);
        switch (request.getHeader().getBizType()){
            case "00":
            default:
                //鉴权
                request.setTxnGroup("2400");
                break;
            case "01":
                //代收
                request.setTxnGroup("2200");
                break;
            case "02":
                //代付
                request.setTxnGroup("2300");
                break;
        }
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        if (!StringUtil.isEmptyOrNull(request.getTransAmt())) {
            String amt;
            try {
                amt = AmountUtil.checkAmt(request.getTransAmt(), 2, false, false);
            } catch (Exception e) {
                rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
                throw new BizException(CopsRespCodeEnum.INVALID_AMT.getRespCode(), CopsRespCodeEnum.INVALID_AMT.getRespDesc());
            }
            request.setTransAmt(amt);
        }
        Class c = Class.forName("cc.rengu.igas.cops.core.model." + CopsProductVersion.VERSION + ".ProcAdditionInfoImpl");
        ProcAddition procAddition = (ProcAddition) c.newInstance();
        request.setAdditionInfo(procAddition.procAdditionInfo(request.getHeader().getInstId(), request.getAdditionInfo()));
        if (!StringUtil.isEmptyOrNull(request.getAdditionInfo())) {
            xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.REMARK4, request.getAdditionInfo());
        }
        return request;
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse bizResponse = new BizResponse();
        IntelliRouterQueryRequest routerQueryRequest = (IntelliRouterQueryRequest) request;
        SmartRouteRequest smartRouteRequest = new SmartRouteRequest();
        BeanUtil.beanCopy(routerQueryRequest, smartRouteRequest);
        smartRouteRequest.setHeader(new cc.rengu.igas.route.facade.base.Header());
        BeanUtil.beanCopy(routerQueryRequest.getHeader(), smartRouteRequest.getHeader());


        if (StringUtil.isEmptyOrNull(routerQueryRequest.getHeader().getTraceNo())) {
            routerQueryRequest.getHeader().setTraceNo(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
            xmlTreeUtil.copyStringValue(TreeNodeConstant.SYS_SEQ_NUM, TreeNodeConstant.HEADER_TRACE_NO);
        }
        //查询不登记订单
        //调用智能路由查询
        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DUBBO_BEAN, DUBBO_METHOD, smartRouteRequest);
        //dubbo调用成功，业务结果不一定为成功
        if (null != result && result.isSuccess()) {
            SmartRouteResponse response = (SmartRouteResponse) result.getResult();
            bizResponse.setResult(response);
            bizResponse.setRespDesc(response.getRespDesc());
            bizResponse.setRespCode(response.getRespCode());
            bizResponse.setRspSysId(AppParamConstant.IGAS);
            ConvertUtil.convertInput(bizResponse.getResult());
        }
        //dubbo调用失败（除超时外的其他异常，可能为服务提供方的异常）
        else if (null == result) {
            bizResponse.setRespCode(CopsRespCodeEnum.COPS_ERROR.getRespCode());
            bizResponse.setRespCode(CopsRespCodeEnum.COPS_ERROR.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.IGAS);
            bizResponse.setTransStatus(TransStatusEnum.TRANS_TIMEOUT);
        }
        //dubbo调用失败，一般不存在
        else {
            bizResponse.setRespCode(CopsRespCodeEnum.COPS_ERROR.getRespCode());
            bizResponse.setRespCode(CopsRespCodeEnum.COPS_ERROR.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.IGAS);
            bizResponse.setTransStatus(TransStatusEnum.SYSTEM_REJECT);
        }
        return bizResponse;
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {

    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }
}
